package com.lyxs916.appbaselibrary.core.http;

import android.support.v4.widget.SwipeRefreshLayout;

import com.lyxs916.appbaselibrary.core.iview.BaseClearView;
import com.lyxs916.appbaselibrary.core.iview.BaseListViewListener;
import com.lyxs916.appbaselibrary.core.iview.BaseSwipeView;
import com.lyxs916.xrecycleview.SuperRecyclerView;
import com.lyxs916.corelib.core.view.CoreBaseView;
import com.lyxs916.network.HttpCallBack;

/**
 * 说明:
 * 作者： 杨阳; 创建于：  2017-06-13  18:01
 */
public class BaseHttpCallback<ResultType> extends HttpCallBack<ResultType> {
    protected SwipeRefreshLayout mSwipeRefreshLayout;
    private SuperRecyclerView mSuperRecyclerView;
    private CoreBaseView mCoreBaseView;

    public BaseHttpCallback(Builder buider) {
        super(buider);

        if (buider instanceof BaseBuilder) {
            mSwipeRefreshLayout = ((BaseBuilder) buider).mSwipeRefreshLayout;
            mSuperRecyclerView = ((BaseBuilder) buider).mSuperRecyclerView;
            mCoreBaseView = ((BaseBuilder) buider).mCoreBaseView;
        }


    }


    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onSuccess(ResultType result) {
        super.onSuccess(result);
        stop();
    }

    @Override
    public void onError(Throwable error) {
        super.onError(error);
        stop();
    }

    /**
     * 停止刷新与加载
     */
    public void stop() {
        //1.清楚数据。2.停止刷新
        if (mSwipeRefreshLayout != null) {
            if (mSwipeRefreshLayout.isRefreshing()) {

                if (mCoreBaseView instanceof BaseClearView) {
                    ((BaseClearView) mCoreBaseView).clearData();
                }
                mSwipeRefreshLayout.setRefreshing(false);
            }
        }
        //加载完成
        if (mSuperRecyclerView != null) {
            mSuperRecyclerView.getStatusChangListener().complete();
        }

    }

    /**
     * 构建BaseHttpCallback
     * <p>
     * 示例
     * HttpCallBack.Builder buider = new BaseHttpCallback.BaseBuilder(mvpView)
     * .setShowLoading((Context) mvpView, true)//显示加载的Dialog
     * .setCancelable(true)//加载的Dialog 可以取消
     * .setHint("正在加载...")//加载时的提示语
     * .setShowDefeatView(false, viewGroup)//加载错误显示的位置，第一个参数表示是否显示加载Dialog
     * .setHostType(HostType.TYPE_TWO);//主机地址
     */
    public static class BaseBuilder extends Builder {
        private SwipeRefreshLayout mSwipeRefreshLayout;
        private SuperRecyclerView mSuperRecyclerView;
        private CoreBaseView mCoreBaseView;

        public BaseBuilder() {
        }

        public BaseBuilder(CoreBaseView coreBaseView) {
            setmCoreOnLoadingBarListener(coreBaseView);

            if (coreBaseView instanceof BaseListViewListener) {
                mSuperRecyclerView = ((BaseListViewListener) coreBaseView).getSuperRecyclerView();
                mSwipeRefreshLayout = mSuperRecyclerView.getSwipeRefreshLayout();
            }

            if (coreBaseView instanceof BaseSwipeView) {
                mSwipeRefreshLayout = ((BaseSwipeView) coreBaseView).getSwipeRefreshLayout();
            }
            mCoreBaseView = coreBaseView;
        }


        /**
         * 下拉刷新的监听
         *
         * @param mSwipeRefreshLayout 刷新控件
         * @return Builder
         */
        public Builder setSwipeRefreshLayout(SwipeRefreshLayout mSwipeRefreshLayout) {
            this.mSwipeRefreshLayout = mSwipeRefreshLayout;
            return this;
        }

    }


}
